package com.xubeizhi.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xubeizhi.common.PageBean;
import com.xubeizhi.common.Result;
import com.xubeizhi.entity.Category;
import com.xubeizhi.mapper.CategoryMapper;
import com.xubeizhi.service.CategoryService;
import com.xubeizhi.utils.DataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public Result createCategory(Category category) {
        BeanUtils.copyProperties(Category.class,category);
        Integer existsByName = baseMapper.existsByName(category.getCategoryName());
        if (existsByName != 0){
            return Result.error(1,"分类名称已存在",null);
        }
        category.setCreateTime(DataUtils.getDateNow());
        category.setUpdateTime(DataUtils.getDateNow());

        int rows = baseMapper.insert(category);
        return rows > 0 ? Result.success(0,"操作成功",rows) : Result.error(1,"操作失败",null);
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public Result updateCategory(Category category) {
        BeanUtils.copyProperties(Category.class,category);
        Integer existsByName = baseMapper.existsByName(category.getCategoryName());
        if (existsByName != 0){
            return Result.error(1,"分类名称已存在",null);
        }
        category.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.updateById(category);
        return rows > 0 ? Result.success(0,"操作成功",rows) : Result.error(1,"操作失败",null);
    }

    /**
     * 删除该分类（假删除，只在前端不显示）
     * @param category
     * @return
     */
    @Override
    public Result removeCategory(Category category) {
        BeanUtils.copyProperties(Category.class,category);
        category.setRemoveTime(DataUtils.getDateNow());
        category.setRemoveStatus("1");
        Integer rows = baseMapper.removeCategory(category);
        if (rows != null){
            return Result.success(0,"删除成功",category);
        }
        return Result.error(1,"删除失败",null);
    }

    /**
     * 下架商品操作
     * @param category
     * @return
     */
    @Override
    public Result soldCategory(Category category) {
        category.setStatus("1");
        Integer rows = baseMapper.soldCategory(category);
        return rows > 0 ? Result.success(0,"操作成功",rows) : Result.error(1,"操作失败",null);
    }

    /**
     * 分页功能
     * @param pageNum
     * @param pageSize
     * @param status
     * @param removeStatus
     * @return
     */
    @Override
    public Result<PageBean<Category>> listCategory(Integer pageNum, Integer pageSize, String status, String removeStatus) {
        PageBean<Category> pb = new PageBean<>();

        PageHelper.startPage(pageNum,pageSize);


        List<Category> categories = baseMapper.listCategory(status,removeStatus);
        Page<Category> p = (Page<Category>) categories;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(0,"查询成功",pb);
    }
}
